from api_test.api.calendar.calendar_api import CalendarApi


class EventApi(CalendarApi):
    """日程管理eventapi统一设置，比如url前缀，工厂方法"""
    def __init__(self):
        super().__init__()
        self.event_ids = []
    # 处理返回结果
    # def reslut(self, res):
    #     print(f"result：{res}")
    #     try:
    #         self.json_data = res.json()
    #         if res.status_code == 200:
    #             print(self.jsonpath(expr="$.code"))
    #             if self.jsonpath(expr="$.code")[0] == 0:
    #                 print(1111)
    #                 print(self.jsonpath(expr="$.data"))
    #                 if self.jsonpath(expr="$.data"):
    #                     # 返回结果$...三层字典结构中找到event_id的值，返回列表
    #                     event_id = self.jsonpath(expr="$...event_id")[0]
    #                     print(self.event_ids)
    #                     self.event_ids.append(event_id)
    #                     return self.json_data
    #                 if not self.jsonpath(expr="$.data"):
    #                     print(self.json_data)
    #                     return self.json_data
    #         else:
    #             print(f"日历创建失败：{res.status_code}{res.text}")
    #             return self.json_data
    #     except Exception as E:
    #         print(E)
    #         print(res.status_code, res.text)

    # 创建日程
    def create(self, calendar_id, summary, description, start_time, end_time):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}/events"
        data = {
            "summary": summary,
            "description": description,
            "start_time": start_time,
            "end_time": end_time,
        }
        res = self.send_request("POST", self.base_url + path, json=data)
        # self.reslut(res)
        # 判断是否创建成功， 成功则记录event_id
        try:
            self.json_data = res.json()
            if res.status_code == 200:
                if self.jsonpath(expr="$.code")[0] == 0:
                    event_id = self.jsonpath(expr="$..event_id")[0]
                    self.event_ids.append(event_id)
                    print(self.event_ids)
                    return self.json_data
            else:
                print(f"日历创建失败：{res.status_code}{res.text}")
                return self.json_data
        except Exception as E:
            print(E)
            print(res.status_code, res.text)

    # 获取日程
    def get(self, calendar_id, event_id):
        url = self.base_url + self.path + calendar_id + "/events/" + event_id
        res = self.eventapi.send("GET", url + event_id)
        return res.json()

    # 获取日程列表
    def list(self, calendar_id):
        url = self.base_url + self.path + calendar_id + "/events/"
        res = self.eventapi.send("GET", url)
        return res.json()

    # 根据 event_id 删除日程
    def delete(self, calendar_id, event_id):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}/events/{event_id}"
        res = self.send_request("DELETE", self.base_url + path)
        print(res.text)
        self.json_data = res.json()
        return self.json_data

    # 根据 event_id 更新日程
    def update(self, calendar_id, event_id, summary, description, start_time, end_time,):
        url = self.base_url + self.path + calendar_id + "/events/" + event_id
        data = {
            "summary": summary,
            "description": description,
            "start_time": start_time,
            "end_time": end_time,
        }
        res = self.eventapi.send("PATCH", url, json=data)
        return res.json()
if __name__ == '__main__':
    ea = EventApi()
    print(ea.calendar_ids)